package cn.edu.ctbu.demo2.service.impl;

import cn.edu.ctbu.demo2.constain.REnum;
import cn.edu.ctbu.demo2.domain.Student;
import cn.edu.ctbu.demo2.domain.Teacher;
import cn.edu.ctbu.demo2.exception.RException;
import cn.edu.ctbu.demo2.repository.TeacherReporsitory;
import cn.edu.ctbu.demo2.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.Pageable;
import java.util.List;

@Service
public class TeacherimplService implements TeacherService {
    @Autowired
    private TeacherReporsitory studentReporsitory;

    public List<Teacher>findAll(){
        return studentReporsitory.findAll();
    }

    //     只支持分页的查询
//    @param pageable
//    @return
    public Page<Teacher> findAll(Pageable pageable){
        return studentReporsitory.findAll(pageable);
    }

    @Override
    public Page<Teacher> finfAll(Example<Teacher> student, Pageable pageable) {
        return studentReporsitory.findAll(student,pageable);
    }

    //    按id进行查询
//    @porom id 主键 整数
//    @return
    public Teacher findById(Integer id){


        return studentReporsitory.findById(id).orElse(null);
    }

    //    按name进行查询,like
//    @porom name 主键 整数
//    @return
    public List<Teacher> findByName(String name){

        return studentReporsitory.findByNameLike(name);
    }


    //    按name和password进行查询
    public List<Teacher>  findByNameAndPassword(String name,String password){

        return studentReporsitory.findByNameAndPassword(name,password);
    }

    public Teacher insert(Teacher student){
        studentReporsitory.save(student);
        return student;
    }

    public Teacher update(Teacher student){
        studentReporsitory.save(student);
        return student;
    }

    public void delete(Teacher student){
        studentReporsitory.delete(student);
    }

    public void delete(Long id){
        Teacher student=new Teacher();
        student.setId(Math.toIntExact(id));
        studentReporsitory.delete(student);
    }

    public void delete(Integer id){
        studentReporsitory.deleteById(id);
    }

    public Teacher getById(Long id){
        Teacher student=studentReporsitory.findById(Math.toIntExact(id)).orElse(null);
        return student;
    }

    public List<Teacher> getByName(String name){
        List<Teacher> students=studentReporsitory.findByNameLike(name);
        return students;
    }

    @Override
    public Teacher validateUserAndPassword(String username, String password) throws Exception{

        List<Teacher> students=studentReporsitory.findBySno(username);
        if(students.size()>0){
            //可能对password加密，但我们暂时不做处理
            Teacher student=students.get(0);
            if(student.getPassword().equals(password)){
                //成功
                return student;
            }
            else {
                throw new RException(REnum.LOGIN_ERR);
            }
        }
        else {
            throw new RException(REnum.LOGIN_ERR);
        }

    }

}
